import java.util.Arrays;

public class test2 {
    //数组逆序
    public static void reverse(int[] array){
        int left =0;
        int right =array.length-1;
        while(left<right){
            int tmp =array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }

    }
    public static void main(String[] args) {
        int[] array = {1,2,3,4};
        reverse(array);
        System.out.println(Arrays.toString(array));
    }
    //求数组的平均值
    public static double avg(int[] array){
        int sum =0;
        for (int x:array){
            sum+=x;
        }
        return (double)sum / (double)array.length;
    }
    public static void main13(String[] args) {
        int[] array = {1,2,3,4,5,6};
        System.out.println(avg(array));
    }
    public static void main12(String[] args) {
        int[][] array ={{1,2,3},{4,5,6}};
        //System.out.println(array.length);//行数
//        System.out.println(array[0].length);
//        System.out.println(array[1].length);
        System.out.println(Arrays.toString(array[0]));
        System.out.println(Arrays.toString(array[1]));
        //二维数组 就是特殊的一维数组
        for (int i=0;i<array.length;i++){
            for (int j=0;j<array[i].length;j++){
                System.out.println(array[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("=======");
        for (int[] tmpArray : array){
            for (int x: tmpArray){
                System.out.println(x+" ");
            }
            System.out.println();
        }
        System.out.println("=======");
        String ret = Arrays.deepToString(array);
        System.out.println(ret);
        int[][]array2 = new int[][]{{1,2,3},{4,2,6}};
        int[][]array3 =new int[2][3];
        int[][] array4 =new int[2][];
    }
    public static void main11(String[] args) {
//        int[] array1 ={1,2,3,4,5};
//        int[] array2 ={1,2,5,4,6};
//        System.out.println(array1==array2);
//        boolean flg = Arrays.equals(array1,array2);
//        System.out.println(flg);
        int[] ret =new int[10];
        Arrays.fill(ret,1,3,-1);
        System.out.println(Arrays.toString(ret));
    }
    //数组的拷贝
    public static int[] copyArray(int[] array){
        int[] copy = new int[array.length];
        for(int i=0;i<array.length;i++){
            copy[i] =array[i];
        }
        return copy;
    }
    public static void main10(String[] args) {
        int[] array = {1,2,31,4,15};
        //int[] copy = Arrays.copyOf(array,array.length*2);
        //int[] copy1 = Arrays.copyOfRange(array,1,3);
        //int[] copy =copyArray(array);
        //System.out.println(Arrays.toString(copy));
        int[] copy =new int[array.length];
        System.arraycopy(array,0,copy,0,array.length);
        System.out.println(Arrays.toString(copy));
    }
    //，冒泡排序
    public static void bubbleSort(int[] array){
        //i代表趟数
        for (int i=0;i<array.length-1;i++){
            boolean flg = false;
            for (int j=0;j<array.length-1-i;j++){
                if(array[j]>array[j+1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg= true;
                }
            }
            if(flg==false){
                return;
            }
        }
    }

    public static void main9(String[] args) {
        int[] array ={1,31,2,5,7,10,9};
        System.out.println("排序前的数组："+Arrays.toString(array));
        bubbleSort(array);//默认从小到大排序
        System.out.println("排序后的数组："+Arrays.toString(array));
    }
    //toString的模拟
    public static String myToString(int[] array){
        String ret ="[";
        for (int i=0;i<array.length;i++){
            ret =ret+array[i];
            if(i !=array.length-1){
                ret +=",";
            }
        }
        ret += "]";
        return ret;
    }

    public static void main8(String[] args) {
        int[] array = {1,2,3,4,5,7};
        System.out.println(myToString(array));
    }
    //查找数组的位置，并显示起下标
    public static int findNum(int[] array,int key){
        for (int i=0;i<array.length;i++){
            if(array[i]==key){
                return i;
            }
        }
        return -1;
    }

    //二分查找->必须是有序的数组
    public static int binarySearch(int[] array,int key){
        int left =0;
        int right =array.length-1;
        while(left<=right){
            int mid =(left+right)/2;
            if(array[mid]==key){
                return mid;
            }else if(array[mid]>key){
                right =mid-1;
            }else{
                left =mid+1;
            }
        }
        return -1;

    }
    public static void main7(String[] args) {
        int[] array={1,2,31,4,16};
        System.out.println("排序前的数组:"+Arrays.toString(array));
        Arrays.sort(array);//默认从小到大排序
        System.out.println("排序后的数组:"+Arrays.toString(array));
        //int index = Arrays.binarySearch(array,1,3,16);
        //int index = binarySearch(array,31);
        int index = findNum(array,2);
        System.out.println(index);
    }
    public static void main6(String[] args) {
        int[] array1 = {1,2,3,4,5};
        array1[0] = 10;
        array1[1] = 20;
        array1[2] = 30;
        int[] array2 = new int[]{1,2,3,4,5};
        array2 [0] =100;
        array2[1] =200;
        array1 = array2;
        array2[2] =300;
        array2[3] =400;
        array2[4] = 500;
        for (int i=0;i<array1.length;i++){
            System.out.println(array1[i]);
        }
    }
    public static int[] func1(int[] array){
        //需求：把数组当中的每个数据 扩大2倍
        int[] ret = new int[array.length];
        for (int i=0;i<array.length;i++){
            ret[i] =array[i]*2;
        }
        return ret;
    }
    //需求：把数据当中的每个数据扩大2倍 -》 数组本身上扩大
    public static void func2(int[] array){
        for (int i=0;i<array.length;i++){
            array[i] = array[i]*2;
        }
    }
    public static void main5(String[] args) {
        int[] array1={1,2,3,4,5};
        func1(array1);
        System.out.println(Arrays.toString(array1));
        func2(array1);
        System.out.println(Arrays.toString(array1));

    }
    public static double max1(double a,double b){
        return a>b?a:b;
    }
    public static double max3(double a,double b,double c){
        int max = (int)max1(a,b);
        double ret = max1(max,c);
        return ret;
    }

    public static void main4(String[] args) {
        System.out.println(max3(1.5,2,4.5));
    }
    public static int add(int a,int b){
        return a+b;
    }
    public static float add(float a,float b,float c){
        return a+b+c;

    }
    public static void main3(String[] args) {
        System.out.println(max3(2,3,5));
        System.out.println(add(1,2,3));
        System.out.println(add(5,6));
    }

    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int[] array2 = new int[]{1,2,3,4,5,6};
        int[] array3 =new int[10];
        //int array4[] = {1,2,3,4,5}; 不建议
        int[] array4;
        array4 =new int[4];
        boolean[] flg= new boolean[3];
    }
    public static void main1(String[] args) {
        int[] array = {1,2,3,4,5};
        for (int i=0;i<array.length;i++){
            System.out.println(array[i]+" ");
        }
        System.out.println();
        System.out.println("=====");
        //for each 循环
        for(int x:array){
            System.out.println(x+" ");
        }
        System.out.println();
        System.out.println("=====");
        //操作数组的工具类 Arrays
        String ret = Arrays.toString(array);
        System.out.println(ret);
    }
}
